//
//  DayFourthViewController.m
//  LockTest
//
//  Created by ZhangBo on 2018/12/4.
//  Copyright © 2018 ZhangBo. All rights reserved.
//

#import "DayFourthViewController.h"
#import "NextViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import <RACReturnSignal.h>


@interface DayFourthViewController ()
@property(nonatomic, weak) IBOutlet UILabel *label;
@property(nonatomic, weak) IBOutlet UITextField *textField;
@end

@implementation DayFourthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)test_rac5 {
    //bind 绑定!
    //1.创建信号
    RACSubject *subject = [RACSubject subject];
    //2.绑定信号
    RACSignal *bindSignal = [subject bind:^RACSignalBindBlock {
        return ^RACSignal *(id value, BOOL *stop) {
            //block调用:只要源信号发送数据,就会调用bindBlock
            //block作用:处理原信号内容
            //value:源信号发送的内容
            NSLog(@"%@", value);
            //返回信号,不能传nil , 返回空信号 :[RACSignal empty]
            return [RACReturnSignal return:value];
        };
    }];

    //3.订阅信号
    [bindSignal subscribeNext:^(id x) {
        NSLog(@"绑定接收到!! %@", x);
    }];

    //4.发送
    [subject sendNext:@"发送原始的数据"];

}

#pragma mark -- RACCommand：用于处理事件的类

- (void)command_demo4 {
    //RACCommand 命令
    //1.创建命令
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id
    input) {
        NSLog(@"%@", input);
        //input:指令
        return [RACSignal createSignal:^RACDisposable *(id <RACSubscriber>
        subscriber) {

            //发送数据
            [subscriber sendNext:@"执行完命令之后产生的数据"];

            //发送完成
            [subscriber sendCompleted];

            return nil;
        }];
    }];
    //监听事件有没有执行完毕
    [command.executing subscribeNext:^(NSNumber *x) {
        if ([x boolValue]) { //正在执行!!
            NSLog(@"正在执行!!");
        } else {
            NSLog(@"已经结束咯&&还没开始做!");
        }
    }];


    RACSignal *signal = [command execute:@"执行!!"];
    [signal subscribeNext:^(id x) {
        NSLog(@"接受到数据了!!%@", x);
    }];

}

- (void)command_demo3 {
    //创建多个信号
    RACSubject *signalOfSignal = [RACSubject subject];
    RACSubject *signal1 = [RACSubject subject];
    RACSubject *signal2 = [RACSubject subject];
    RACSubject *signal3 = [RACSubject subject];

    //订阅信号
    //    [signalOfSignal subscribeNext:^(id   x) {
    //       [x subscribeNext:^(id   x) {
    //           NSLog(@"%@",x);
    //       }];
    //    }];

    //switchToLatest :最新的信号!!
    [signalOfSignal.switchToLatest subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];


    //发送信号
    [signalOfSignal sendNext:signal1];
    [signalOfSignal sendNext:signal2];
    [signalOfSignal sendNext:signal3];
    //发送数据
    [signal3 sendNext:@"3"];
    [signal2 sendNext:@"2"];
    [signal1 sendNext:@"1"];

}

- (void)command_demo2 {
    //RACCommand 命令
    //1.创建命令
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id
    input) {
        NSLog(@"%@", input);
        //input:指令
        return [RACSignal createSignal:^RACDisposable *(id <RACSubscriber>
        subscriber) {

            //发送数据
            [subscriber sendNext:@"执行完命令之后产生的数据"];

            return nil;
        }];
    }];

    //订阅信号
    //executionSignals:信号源!!,发送信号的信号!
    //    [command.executionSignals subscribeNext:^(RACSignal * x) {
    //        [x subscribeNext:^(id   x) {
    //            NSLog(@"%@",x);
    //        }];
    //        NSLog(@"%@",x);
    //    }];

    //switchToLatest 获取最新发送的信号.
    [command.executionSignals.switchToLatest subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];


    //2.执行命令
    [command execute:@"输入的指令!!"];
}

- (void)command_demo1 {
    //1.创建命令
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id
    input) {
        NSLog(@"a:%@", input);
        //input:指令
        //
        return [RACSignal createSignal:^RACDisposable *(id <RACSubscriber>
        subscriber) {
            //发送数据
            [subscriber sendNext:@"执行完命令之后产生的数据"];
            return nil;
        }];
    }];

    //2.执行命令
    RACSignal *signal = [command execute:@"输入的指令!!"];
    //3.订阅信号!
    [signal subscribeNext:^(id x) {
        NSLog(@"b:%@", x);
    }];

}

- (void)test_rac4 {
//    [self command_demo1];
//    [self command_demo2];
//    [self command_demo3];
    [self command_demo4];
}

#pragma mark -- RACMulticastConnection：处理重复发送消息的问题

- (void)test_rac3 {
    //不管订阅多少次信号,就只会请求一次数据
    //RACMulticastConnection:必须要有信号
    //1.创建信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber>
    subscriber) {

        //发送网络请求
        NSLog(@"发送请求");
        //发送数据
        [subscriber sendNext:@"请求到的数据"];

        return nil;
    }];

    //2.将信号转成连接类!!
    RACMulticastConnection *connection = [signal publish];

    //3.订阅连接类的信号
    [connection.signal subscribeNext:^(id x) {
        NSLog(@"A处在处理数据%@", x);
    }];
    [connection.signal subscribeNext:^(id x) {
        NSLog(@"B处在处理数据%@", x);
    }];

    //4.连接
    [connection connect];
}

#pragma mark -- RAC常用的宏定义

- (void)test_rac2 {
    //监听文本框内容
//        [_textField.rac_textSignal subscribeNext:^(NSString *  x) {
//
//            _label.text = x;
//        }];
    //1.RAC(对象，对象属性)：绑定属性
    //给某个对象的某个属性绑定信号,一旦信号产生数据,就会将内容赋值给属性!
//    RAC(self.label,text) = _textField.rac_textSignal;

    //2.RACObserve(被观察的对象，被观察对象的属性) ：代替KVO监听某个对象的某个属性
    //只要这个对象的属性发生变化..哥么信号就发送数据!!
//    [RACObserve(self.label, text) subscribeNext:^(id   x) {
//        NSLog(@"label:%@",x);
//    }];

    //3.RACTuplePack与RACTupleUnpack
    //包装元祖
//    RACTuple * tuple = RACTuplePack(@1,@2);
//    NSLog(@"%@",tuple[0]);

    //解包!!
    //    RACTupleUnpack(<#...#>)

    //4.@weakify、@strongify
    //循环引用
        NextViewController *next = [[NextViewController alloc] init];
        [self presentViewController:next animated:YES completion:nil];

}

#pragma mark -- 多请求汇总处理 rac_liftSelector：withSignals：

- (void)test_rac1 {
    //请求1
    RACSignal *signal1 = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber>
    subscriber) {
        //发送请求
        NSLog(@"请求网络数据 1");
        [NSThread sleepForTimeInterval:1];
        //发送数据
        [subscriber sendNext:@"数据1 来了"];
        return nil;
    }];

    //请求2
    RACSignal *signal2 = [RACSignal createSignal:^RACDisposable *(id <RACSubscriber>
    subscriber) {
        //发送请求
        NSLog(@"请求网络数据 2");
        [NSThread sleepForTimeInterval:2];
        //发送数据
        [subscriber sendNext:@"数据2 来了"];
        return nil;
    }];

    //数组:存放信号
    //当数组中的所有洗好都发送了数据,才会执行Selector
    //方法的参数:必须和数组的信号一一对应!!
    //方法的参数:就是每一个信号发送的数据!!
    [self rac_liftSelector:@selector(updateUIWithOneData:TwoData:) withSignalsFromArray:@[signal1, signal2]];
}

- (void)updateUIWithOneData:(id)oneData TwoData:(id)twoData {
    NSLog(@"%@", [NSThread currentThread]);
    //拿到数据更新UI
    NSLog(@"UI!!%@%@", oneData, twoData);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self test_rac1];
    [self test_rac2];
//    [self test_rac3];
//    [self test_rac4];
//    [self test_rac5];
}

@end
